package com.ym.controller.common.auth;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.ym.common.constant.Constants;
import com.ym.common.utils.HttpUtil;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.config.ConfigCenterProperties;
import com.ym.core.config.YinLianConfig;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.domain.param.BankCardInfoParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 银联认证
 */
@Api(tags = "银联认证")
@RestController
@ApiSign
@RequestMapping(value = "/common/auth/yinlian", headers = Constants.Api.VERSION)
public class YinLianAuthController extends BaseController {

	@Autowired
	private ConfigCenterProperties configCenterProperties;


	@ApiOperation("获取token")
	@GetMapping("/getToken")
	public APIResponse getToken() {
		YinLianConfig yinLianConfig = configCenterProperties.getYinLianConfig();
		RestTemplate restTemplate = new RestTemplate();
		String getTokenUrl = "https://openapi.unionpay.com/upapi/cardbintest/token?app_id=" + yinLianConfig.getAppId() + "&app_secret=" + yinLianConfig.getAppSecret();
		String strResult = restTemplate.getForObject(getTokenUrl, String.class);
		JSONObject result = JSONObject.parseObject(strResult);
		if ("0000".equals(result.get("respCd")) && "成功".equals(result.get("respMsg"))) {
			return successData(result);
		}
		return failMsg("app_id与app_secret验证失败");
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("银行卡信息查询")
	@GetMapping("/getCardInfo")
	public APIResponse getCardInfo(@ApiParam(name = "cardNo", value = "银行卡号", required = true) @RequestParam String cardNo,
								   @ApiParam(name = "token", value = "银联token", required = true) @RequestParam String token) {
		YinLianConfig yinLianConfig = configCenterProperties.getYinLianConfig();
		/*签名*/
		String body = com.alibaba.fastjson.JSONObject.parseObject(cardNo).toString();
		long now = System.currentTimeMillis();
		String ts = String.valueOf(now);
		String signature = yinLianConfig.getSignature();
		// todo 签名规则不知,先MD5一下
		String sign = SecureUtil.md5(body + ts + signature);

		RestTemplate restTemplate = new RestTemplate();
		Map<String, String> params = new HashMap<>();
		params.put("cardNo", cardNo);

		String getCardInfoUrl = "https://openapi.unionpay.com/upapi/cardbintest/cardinfo?token=" + token + "&sign=" + sign + "&ts=" + now;
		String strResult = restTemplate.postForObject(getCardInfoUrl, params, String.class);
		JSONObject result = JSONObject.parseObject(strResult);
		if ("0000".equals(result.get("respCd")) && "成功".equals(result.get("respMsg"))) {
			return successData(result);
		}
		return failMsg("银行卡信息查询失败");
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("验证银行卡信息")
	@GetMapping("/auth/cardInfo")
	public APIResponse authCardInfo(@ApiParam(name = "cardNo", value = "银行卡号", required = true) @RequestParam String cardNo,
									@ApiParam(name = "token", value = "银联token", required = true) @RequestParam String token) {
		YinLianConfig yinLianConfig = configCenterProperties.getYinLianConfig();
		/*签名*/
		String body = com.alibaba.fastjson.JSONObject.parseObject(cardNo).toString();
		long now = System.currentTimeMillis();
		String ts = String.valueOf(now);
		String signature = yinLianConfig.getSignature();
		// todo 签名规则不知,先MD5一下
		String sign = SecureUtil.md5(body + ts + signature);

		RestTemplate restTemplate = new RestTemplate();
		Map<String, String> params = new HashMap<>();
		params.put("appId", yinLianConfig.getAppId());
		params.put("appPwd", cardNo);
		params.put("cardNo", cardNo);

		String getCardInfoUrl = "https://auth.95516.com/authonl/onlineAuth/rest/verify/bankcard";
		String strResult = restTemplate.postForObject(getCardInfoUrl, params, String.class);
		JSONObject result = JSONObject.parseObject(strResult);
		if ("0000".equals(result.get("respCd")) && "成功".equals(result.get("respMsg"))) {
			return successData(result);
		}
		return failMsg("银行卡信息查询失败");
	}

	@Log
	@ApiOperation("银行卡信息查询")
	@GetMapping("/auth/bankCardInfo")
	public APIResponse authBankCardInfo(@ApiParam(name = "bankCardInfo", value = "银行卡信息", required = true)
												BankCardInfoParam bankCardInfo) {
		String host = "https://ali-bankcard4.showapi.com/bank4";
		String appcode = configCenterProperties.getBankCardConfig().getAppcode();

		Map<String, String> querys = new HashMap<String, String>();
		querys.put("acct_name", bankCardInfo.getAcct_name());
		querys.put("acct_pan", bankCardInfo.getAcct_pan());
		querys.put("cert_id", bankCardInfo.getCert_id());
		querys.put("phone_num", bankCardInfo.getPhone_num());
		querys.put("cert_type", bankCardInfo.getCert_type());
		querys.put("needBelongArea", bankCardInfo.getNeedBelongArea());


		String result = HttpUtil.get(host, querys, false).header("Authorization", "APPCODE " + appcode).execute();
		JSONObject jsonResult = JSONObject.parseObject(result);
		String showapi_res_code = jsonResult.get("showapi_res_code") + "";
		if (jsonResult != null && ("0".equals(showapi_res_code))) {
			JSONObject resBody = JSONObject.parseObject(jsonResult.getString("showapi_res_body"));
			String code = resBody.get("code") + "";
			if (resBody != null && ("0".equals(code))) {
				Object belong = resBody.get("belong");
				return successData(belong);
			}
		}
		return failMsg("资料不匹配");
	}


}
